package tlcn.business;

import java.util.ArrayList;

import tlcn.data.Dish;
import tlcn.data.Material;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class ManagerData {
	Context context;
	OpenHelper helper;
	public ManagerData(Context context) {
		this.context = context;
		helper= new OpenHelper(context);
	}
	
	public ArrayList<Material> getMaterialOfDish(String DishID){
		ArrayList<Material> materials= new ArrayList<Material>();
		SQLiteDatabase database= helper.getReadableDatabase();
		String sql=
			"select Materials.MaterialID,MaterialName,Unit,Weight "+
			"from Materials join MaterialsOfDish on Materials.MaterialID=MaterialsOfDish.MaterialID "+
			"where DishID='"+DishID+"'";
		Cursor c= database.rawQuery(sql, null);
		while (c.moveToNext()) {
			String materialID= c.getString(c.getColumnIndex("MaterialID"));
			String materialName= c.getString(c.getColumnIndex("MaterialName"));
			String unit= c.getString(c.getColumnIndex("Unit"));
			int weight= c.getInt(c.getColumnIndex("Weight"));
			materials.add(new Material(materialID, materialName, unit, weight));
		}
		return materials;
	}
	
	public ArrayList<Dish> getDishes(){
		ArrayList<Dish> dishes= new ArrayList<Dish>();
		SQLiteDatabase database= helper.getReadableDatabase();
		Cursor c= database.query("Dishes", null, null,null,null, null, null, null);
		while (c.moveToNext()) {
			String id= c.getString(c.getColumnIndex("DishID"));
			String name= c.getString(c.getColumnIndex("DishName"));
			String guide= c.getString(c.getColumnIndex("Guide"));
			String video= c.getString(c.getColumnIndex("Video"));
			String type= c.getString(c.getColumnIndex("Type"));
			dishes.add(new Dish(id, name, guide, video, type));
		}
		return dishes;
	}
	
	public ArrayList<Dish> getDishesByName(String namedish){
		ArrayList<Dish> dishes= new ArrayList<Dish>();
		SQLiteDatabase database= helper.getReadableDatabase();
		Cursor c= database.query("Dishes", null, null,null,null, null, null, null);
		while (c.moveToNext()) {
			String id= c.getString(c.getColumnIndex("DishID"));
			String name= c.getString(c.getColumnIndex("DishName"));
			String guide= c.getString(c.getColumnIndex("Guide"));
			String video= c.getString(c.getColumnIndex("Video"));
			String type= c.getString(c.getColumnIndex("Type"));
			dishes.add(new Dish(id, name, guide, video, type));
		}
		return dishes;
	}

}
